﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DevComponents.DotNetBar.Controls;
using System.Data;

namespace BUL
{
    public partial class Business
    {      
        public void GetAllCongDung(string strTimkiem, DataGridViewX dataGridViewX )
        {
            if(dataGridViewX.RowCount>0)
                dataGridViewX.Rows.Clear();
            using (var dal = new QuanLyBanHangEntities1())
            {
                var listResult = dal.tblCongDungs.Where(x => x.TenCongDung.Contains(strTimkiem)).OrderBy(x => x.TenCongDung);
                int i = 0;
                foreach (var item in listResult)
                {
                    dataGridViewX.Rows.Add();
                    dataGridViewX.Rows[i].Cells[0].Value = "N";
                    dataGridViewX.Rows[i].Cells[1].Value = item.MaCongDung;
                    dataGridViewX.Rows[i].Cells[2].Value = item.TenCongDung;
                    dataGridViewX.Rows[i].Cells[3].Value = item.GhiChu;
                    i++;
                }
            }
        }

        public tblCongDung GetCongDungById(string macongdung)
        {
            return _dal.tblCongDungs.Where(x => x.MaCongDung == macongdung).FirstOrDefault();
        }

        public void InsertCongDung(tblCongDung congdung)
        {
            _dal.tblCongDungs.AddObject(congdung);
            _dal.SaveChanges();
        }

        public void UpdateCongDung(string maCongDungCu, tblCongDung congdung)
        {
            var CDUpdate = _dal.tblCongDungs.Where(x => x.MaCongDung == maCongDungCu).FirstOrDefault();
            CDUpdate.MaCongDung = congdung.MaCongDung;
            CDUpdate.TenCongDung = congdung.TenCongDung;
            CDUpdate.GhiChu = congdung.GhiChu;
            _dal.SaveChanges();
        }

        public void DeleteCD(IEnumerable<string> macongdungs)
        {
            var listDelete = _dal.tblCongDungs.Where(x => macongdungs.Contains(x.MaCongDung));
            foreach (var tblcongdung in listDelete)
            {
                _dal.tblCongDungs.DeleteObject(tblcongdung);
            }
            _dal.SaveChanges();
        }

        public tblCongDung BuildCD(string maCD, string tenCD, string ghichu)
        {
            return new tblCongDung { MaCongDung = maCD, TenCongDung = tenCD,GhiChu = ghichu };
        }
    }
}
